//
//  main.swift
//  zcliperd
//
//  Created by 张宇飞的电脑 on 2019/9/5.
//  Copyright © 2019 zyf. All rights reserved.
//

import Foundation



var service: ClipService? = nil

func handleSignal(_ signum: Int32) {
    print("get signal \(signum) and exiting daemon")
    service?.stop()
}

func showHelp() {
    let url = URL(fileURLWithPath: CommandLine.arguments[0])
    print("用法：\(url.lastPathComponent) <保存历史的数量> <通信用的 unix socket 文件路径>")
    exit(0)
}

// 判断参数
if CommandLine.argc < 3 {
    showHelp()
}

guard let historyCount: Int = Int(CommandLine.arguments[1]) else {
    print("参数错误，参数 1 需要是数字")
    exit(0)
}

if historyCount < 1 {
    print("参数错误，参数 1 需要大于 0")
    exit(0)
}

let sockPath = CommandLine.arguments[2]

ZDaemon.daemonize()

signal(SIGTERM, handleSignal)
signal(SIGINT, handleSignal)
service = ClipService(sockPath, historyCount)
//service = ClipService("/tmp/zcliper.sock", 10)
service?.run()

